<?php
/**
 * Edit page content
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo
 */
class Minipage_AppController extends Zend_Controller_Action
{

    public function init()
    {
    }
    /**
     * Show page content
     *
     * @return void
     */
    public function indexAction()
    {
        $this->setCss('form');
        $this->setCss('window');

        $layout = Zend_Layout::getMvcInstance();
        $layout->appSubmit = array('<a href="javascript: submitform(\'minipage\')">speichern</a>');
        $layout->appTab = Minipage_Model_Tab::render($this->view);

        $homePath = 'home/default/minipage';
        // Load header data
        $head = new Zend_Config_Ini($homePath . '/index.ini', 'head');
        // Read page content
        $content = file_get_contents($homePath . '/index.phtml');

        $form = new Minipage_Form_Edit;
        $buildForm = $form->build($head, $content);

        $request = $this->_request->getPost();

        if($request) {

            if($buildForm->isValid($request)) {
                $this->_forward('save');
            }
        }
        $this->view->form = $buildForm;
    }
    /**
     * Save new page content
     *
     * @throws Exception Änderung konnte nicht gespeichert werden!
     * @return void
     */
    public function saveAction() {
        try {
            $content = $this->_request->getPost();

            if(Minipage_Model_Page::save($content) === FALSE) {
                throw new Exception(
                    'Änderung konnte nicht gespeichert werden!'
                );
            }

        } catch (Exception $e) {

            $this->_forward(
                'error',
                null,
                null,
                array('errorMessage' => $e->getMessage())
            );
        }
    }
    /**
     * Error output
     *
     * @return void
     */
    public function errorAction() {

        $layout = Zend_Layout::getMvcInstance();
        // Delete save button
        $layout->appSubmit = array();

        $this->setCss('error');
        $this->view->errorMessage = $this->_request->errorMessage;
    }
    protected function setCss($file) {

        $cssPath = $this->view->baseUrl('application/modules/minipage/public/css');
        $this->view
             ->headLink()
             ->appendStylesheet(
                 $cssPath . DIRECTORY_SEPARATOR .
                 $file . ".css"
               );
    }
}